/**
 * Created by zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/8/7
 * Time: 上午5:44
 */
$(function () {

    sendAjax('/v1/regions.json', 'get', {}, function(json){
        var regions = json.body.rows;
        var select_region = '<select class="form-control select-region"><option value="">所在地</option>';
        $.each(regions, function(n, v){
            select_region += '<option value="'+v.id+'">'+v.name+'</option>';
        });
        select_region += '</select>';
        var toolbar = '<div class="row">';
        toolbar += '<div class="col-sm-12">';
        toolbar += '<div class="col-sm-1">';
        toolbar += '<a class="btn btn-sm btn-success edit-apart" href="/manager/apart/create.html">添加</a>';
        toolbar += '</div>';
        toolbar += '<div class="col-sm-1">';
        toolbar += '</div>';
        toolbar += '<div class="col-sm-10">';
        toolbar += '<div class="row">';
        toolbar += '<div class="col-sm-12">';
        toolbar += '<div class="col-sm-6">';
        toolbar += select_region;
        toolbar += '</div>';
        toolbar += '<div class="col-sm-6">';
        toolbar += '<select class="form-control select-status"><option value="">状态</option><option value="1">未开放</option><option value="2">已开放</option><option value="3">租用中</option><option value="4">维修中</option></select>';
        toolbar += '</div>';
        toolbar += '</div>';
        toolbar += '</div>';
        toolbar += '</div>';
        toolbar += '</div>';
        toolbar += '</div>';
        loadList('/v1/manager/apart/list.json', toolbar);
        $('#delete-apart-dialog').on('show.bs.modal', function(event){
            var button = $(event.relatedTarget);
            var id = button.data('id');
            var name = button.data('name');
            $(this).find('.apart-id').val(id);
            $(this).find('.apart-name').text(name);
        });
    });

    $(document).on({
        change: function () {
            refreshList();
        }
    },'.select-region,.select-status');

    $('#delete-apart-dialog').on('hide.bs.modal', function(){
        $(this).find('.apart-id').val('');
        $(this).find('.apart-name').text('');
    });
    $('#delete-apart-dialog .btn-save').on('click', function(){
        var id = $('#delete-apart-dialog').find('.apart-id').val();
        sendAjax('/v1/manager/delete/apart', 'post', {id:id}, function(json){
            $('#delete-apart-dialog').modal('hide');
            $('#action-tip').find('.tip-content').text(json.message);
            $('#action-tip').modal('show');
        });
    });

    $('#action-tip').on('hide.bs.modal', function(){
        $(this).find('.tip-content').text('');
        location.reload();
    });

    $('.apart-import').ajaxUpload({
        url: '/v1/upfile',
        name:'file-import',
        onSubmit:function (data){
            $('#upload-tip').html('正在上传中...');
            return true;
        },
        onComplete:function (json){
            if(json.status==200){
                var info = json.body.info[0];

                $('#upload-tip').html('上传成功，正在处理...');
                sendAjax('/v1/manager/import/apart', 'post', {file: info.savename}, function (json) {
                    if (json.status==200) {
                        $('#upload-tip').html('导入完成');
                        refreshList();
                    } else {
                        alert(json.message);
                    }
                });
            }else{
                BootstrapDialog.show({
                    title: '温馨提示',
                    message: '<div class="empty">'+ json.message +'</div>',
                    cssClass: 'modal-lol-ui'
                });
            }
        }
    });
});
function loadList(url, toolbar) {
    $("#apart-table").bootstrapTable({
        url: url,
        ajaxOptions:{
            headers: {"Authorization": $.cookie()._token}
        },
        pagination: true,
        toolbar: toolbar,
        iconSize: "outline",
        pageList: [10, 20, 50, 100],
        sortable: true,
        search: true,
        sortOrder: 'desc',
        striped: true,
        sortStable: true,
        sidePagination: 'server',
        queryParams : function (params) {
            params.region = $('.select-region').val();
            params.status = $('.select-status').val();
            return params;
        },
        icons: {
            refresh: "glyphicon-repeat",
            columns: "glyphicon-list"
        },
        columns: [{
            field: 'id',
            title: 'ID'
        }, {
            field: 'name',
            title: '公寓名称'
        }, {
            field: 'region_name',
            title: '所在地'
        }, {
            field: 'type',
            title: '类型',
            formatter: function (value) {
                switch (value) {
                    case 1 : return '一居室';
                    case 2 : return '一室一厅';
                    case 3 : return '两室一厅';
                    case 4 : return '三室一厅';
                    case 5 : return '三室两厅';
                    default : return '一居室';
                }
            }
        }, {
            field: 'area',
            title: '面积',
            formatter: function (value) {
                return value + '平方米';
            }
        },  {
            field: 'status',
            title: '状态',
            formatter: function (value, row) {
                return getStatus(value, row);
            }
        }, {
            field: 'id',
            title: '操作',
            formatter: function (value, row) {
                var action = '<a class="btn btn-xs btn-info view-apart" href="/manager/apart/view/'+row.id+'.html">详情</a>';
                if (row.status == 1) {
                    action += '<a class="btn btn-xs btn-success edit-apart" href="/manager/apart/edit/'+row.id+'.html">编辑</a>';
                    //action += '<a class="btn btn-xs btn-primary open-apart" data-toggle="modal" data-target="#" data-id="'+row.id+'">开放非项目房源</a>';
                }
                action += '<a class="btn btn-xs btn-danger delete-apart" data-toggle="modal" data-target="#delete-apart-dialog" data-id="'+row.id+'" data-name="'+row.name+'">删除</a>';
                return action;
            }
        }, {
            field: 'is_program',
            title: '是否项目房源',
            visible: false
        }]
    });
}

function refreshList() {
    $('#apart-table').bootstrapTable('refresh');
}

function getStatus(status, row) {
    var back = '';
    switch (status) {
        case 1 : back = '未开放';break;
        case 2 : back = '已开放';break;
        case 3 : back = '租用中';break;
        case 4 : back = '维修中';break;
        default : back = '未开放';break;
    }
    if (status == 2 && row.is_program == 1) {
        back = '项目中';
    }
    return back;
}